package com.example.barcodescanner;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends Activity {
	ImageButton button;
	TextView scanResultText;
	Button go;
	Button send;
	EditText number;
	EditText msg;
	String qrcontent="";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		button = (ImageButton) findViewById(R.id.button1);

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				addListenerOnButton();		

			}

		});
		
		
		// Toast toast = Toast.makeText(getApplicationContext(), "Hello World",
		// Toast.LENGTH_LONG);
		// toast.show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case IntentIntegrator.REQUEST_CODE: {
			if (resultCode != RESULT_CANCELED) {
				IntentResult scanResult = IntentIntegrator.parseActivityResult(
						requestCode, resultCode, data);
				if (scanResult != null) {
					qrcontent = scanResult.getContents();

					// put whatever you want to do with the code here
					//scanResultText = (TextView) findViewById(R.id.qrData);
					//scanResultText.setText(qrcontent);

					/*Intent browserIntent = new Intent(
					Intent.ACTION_VIEW, Uri.parse(qrcontent));
			startActivity(browserIntent);*/
					
					 HttpClient httpclient = new DefaultHttpClient();  
				        
				        HttpGet request = new HttpGet(qrcontent);
				       // System.out.println("url is" + URL + q);
				        
				        String result=null;
				        
				        //request.addHeader("Content-type","applicaiton/json");
				        request.addHeader("Accept","application/json");
				        try{
				        
				        ResponseHandler<String> handler = new BasicResponseHandler();  
				          
				            result = httpclient.execute(request, handler);  
				            
				            System.out.println(result);
				            
				            
				   				
				   				
				        httpclient.getConnectionManager().shutdown();  
				        //Log.i(tag, result);  
					
					
				        }
				        catch(Exception ex)
				        {
				        ex.printStackTrace();
				        }
					
					
					/////////////
					//Toast toast = Toast.makeText(getApplicationContext(),"result is"+result ,
						//	 Toast.LENGTH_LONG);
							// toast.show();
							 
							 Intent browserIntent = new Intent(getApplicationContext(),
										DetailsActivity.class);
							 browserIntent.putExtra("data",result);
							 
								startActivity(browserIntent);
					
					
					
						}
										}
				
			break;
		}
		}
	}


	
	
	
	public void addListenerOnButton() {

		IntentIntegrator.initiateScan(this);

	}

}
